package com.eusoft.grades.school;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import com.eusoft.grades.school.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.LinkedList;

/**
 * Utilities class for use with GUI for importing and exporting profiles
 * with the Android Platform
 * @author nickeubanks
 */
public class SyncAndroid {

    /**
     * Send student object from School package to Android Device SDCard
     * @param deskStudent Student being exported from Desktop
     * @param pathToDroidSDRoot Path to the Device's SDCard
     * @param profileName Name to save profile as
     */
    public static void syncToDroid(Student deskStudent, String pathToDroidSDRoot, String profileName) {

        String osName = System.getProperty("os.name").toLowerCase();
        boolean isMacOs = osName.startsWith("mac os x");

        if (isMacOs) {
            File f = new File("/Volumes");
            pathToDroidSDRoot = "/Volumes/" + pathToDroidSDRoot;


        } else {

        }//TODO: Add windows/linux paths

        System.out.println("Creating Student, PathToDroidSRRoot: " + pathToDroidSDRoot);
        //TODO: Add import stuff
        


        try {
            File root = new File(pathToDroidSDRoot);//Environment.getExternalStorageDirectory();
            if (root.canWrite()) {
                File file = new File(root, "/Grades/");//Use for external data: "/Android/data/com.eusoft.grades/files/");
                file.mkdirs();
                file = new File(root, "/Grades/CurrentProfile/" + profileName + ".p");//use for external data: "/Android/data/com.eusoft.grades/files/student.p");
                file.createNewFile();

                FileOutputStream out = new FileOutputStream(file.getAbsolutePath());
                ObjectOutputStream s = new ObjectOutputStream(out);
                s.writeObject(deskStudent);
                s.flush();
                out.close();

            }

            //System.out.println("Finished Export: To Load this profile on your phone go to Utilities and Import: " + profileName + ".p");
        } catch (Exception e) {
            System.err.println("Error Accessing SD Card");
        }

    }

    /**
     * Allows for importing student from Android's com.eusoft.grades.school package to the Desktop app
     * @param pathToDroidSDRoot Path to the Device's SDCard
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static Student syncToDesktop(String pathToDroidSDRoot) throws IOException, ClassNotFoundException {
        //Load AStudent
        Student aStudent = new Student();
        boolean success = true;
        try {

            String osName = System.getProperty("os.name").toLowerCase();
            boolean isMacOs = osName.startsWith("mac os x");

            if (isMacOs) {
                File f = new File("/Volumes");
                pathToDroidSDRoot = "/Volumes/" + pathToDroidSDRoot;


            } else {
            }//TODO: Add windows/linux paths

            System.out.println("Path To Root: " + pathToDroidSDRoot);
            File file = new File(pathToDroidSDRoot, "/Grades/CurrentProfile/student.p"); //Use for internal storoage"/Android/data/com.eusoft.grades/files/student.p");
            System.out.println("Retrieved File: " + file.getAbsolutePath());
            FileInputStream in = new FileInputStream(file.getAbsolutePath());
            ObjectInputStream s = new ObjectInputStream(in);
            System.out.println("Set up streams");
            aStudent = (Student) s.readObject();
            System.out.println("Created Student");
            
           


        } catch (Exception e) {
            System.err.println(e.getStackTrace() + "\n" + e.getMessage());
            success = false;
        }

        return aStudent;
    }
}
